<?php

namespace Agent\SimBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Agent\SimBundle\Entity\Sim;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class DefaultController extends Controller
{
    public function indexAction($name)
    {
        return $this->render('AgentSimBundle:Default:index.html.twig', array('name' => $name));
    }
    

    public function createAction() {
        $sim = new Sim();
        $sim->setNumber('123456');
        $sim->setPuk('5555555');
        $sim->setCreatedDate(new \DateTime("now"));
        $sim->setUpdatedDate(new \DateTime('now'));

        $em = $this->getDoctrine()->getEntityManager();
        $em->persist($sim);
        $em->flush();

        return new Response('Created product id ' . $sim->getId());
        //return new Response("test");
    }

    
    public function showAction($id)
    {
        $sim = $this->getDoctrine()->getRepository('AgentSimBundle:Sim')->find($id);
//        echo '<pre>';
//        print_r($sim);
//        echo '</pre>';
//        exit();
        if (!$sim) {
            throw $this->createNotFoundException('Номер не найден!');
        }

/*        
 *      $html = <<<HTML
        <h1>{$sim->getNumber()}</h1>

        <p>{$sim->getPuk()}</p>

        <hr/>
        <small>Запись создана {$sim->getCreatedDate()->format("Y-m-d H:i:s")}</small>
HTML;

        return new Response($html);
 * 
 */
        //return new JsonResponse($sim);
        //return $this->render($sim);
        return new Response(array('sim' => $sim));
    }    
}
